package com.dotstone.chipism.util;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.dotstone.chipism.bean.City;
import com.dotstone.chipism.bean.District;
import com.dotstone.chipism.bean.Province;

/*************************************************************************
 * 公       司： 深圳市高金科技有限公司
 * 作       者： 深圳市高金科技有限公司
 * 文件名称：XmlParserHandler.java
 * 内容摘要：
 * 日        期： 2016-11-28
 ************************************************************************/
public class XmlParserHandler extends DefaultHandler {

	private List<Province> provinces = new ArrayList<Province>();

	public XmlParserHandler() {

	}

	public List<Province> getDataList() {
		return provinces;
	}

	@Override
	public void startDocument() throws SAXException {

	}

	Province province = new Province();
	City city= new City();
	District district = new District();

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		if (qName.equals("province")) {
			province = new Province();
			province.setName(attributes.getValue(0));
			province.setCitys(new ArrayList<City>());
		} else if (qName.equals("city")) {
			city = new City();
			city.setName(attributes.getValue(0));
			city.setDistricts(new ArrayList<District>());
		} else if (qName.equals("district")) {
			district = new District();
			district.setName(attributes.getValue(0));
			district.setZipcode(attributes.getValue(1));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		if (qName.equals("district")) {
			city.getDistrictList().add(district);
		} else if (qName.equals("city")) {
			province.getCitys().add(city);
		} else if (qName.equals("province")) {
			provinces.add(province);
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
	}

}
